package com.itkaien.demoCustomerRegisterBean;

import java.lang.reflect.Proxy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class BeanPostMethod implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName+" 初始化之前");
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Object o = Proxy.newProxyInstance(
                bean.getClass().getClassLoader(), bean.getClass().getInterfaces(), (proxy, method, args) -> {
                    System.out.println(beanName + " 执行了增强方法");
                    //执行原方法
                    Object invoke = method.invoke(bean, args);
                    return invoke;
                }
        );


        System.out.println(beanName+" 初始化之后");
        return BeanPostProcessor.super.postProcessAfterInitialization(o, beanName);
    }
}

